

//marriage and fertility plots by race
import delimited "$model/simulated_data/simulated_data_base_2.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
ren v16 sim 
ren v17 a_max
ren v18 tau
ren v19 util
bys uniqid sim: egen work_ever = max(h)
//fix up wages as we do in data
su wage, d
replace wage = 30.99 if wage>30.99
replace wage = 7.25 if wage<7.25 & wage!=0 & wage!=. //
gen race = 1
tempfile whites
save `whites'



import delimited "$model/simulated_data/simulated_data_base_3.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
ren v16 sim 
ren v17 a_max
ren v18 tau
ren v19 util
bys uniqid sim: egen work_ever = max(h)
//fix up wages as we do in data
su wage, d
replace wage = 30.99 if wage>30.99
replace wage = 7.25 if wage<7.25 & wage!=0 & wage!=. //
gen race = 2
append using `whites'
save "$temp/sample2", replace





import delimited "$model/utilities/estimation_sample_whites.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
gen race = 1
tempfile whites
drop if e == 2 & age<4

save `whites'


import delimited "$model/utilities/estimation_sample_blacks.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
gen race = 2
drop if e == 2 & age<4

append using `whites'
save "$temp/sample1", replace




//garrett messed up some stuff when creating the estimation sample in terms of how variables are coded (indices vs. values). Adjust here.
forval i = 1/2{
	use "$temp/sample`i'", clear
	ds e m p f
	foreach var in `r(varlist)'{
		replace `var' = `var' - 1
	}
	replace ac = ac - 2
	replace x = x-1 //experience: 0 is the first index 
	replace age = age+21
	replace wage = 7.25 if wage<7.25 & wage!=0 & wage!=. //
	replace wage = . if wage == 0 //not observed if zero
	
	//variables to collapse: x, f, m (binary) h, wage, migration (binary)
	//collapse by education and age
	gen married = (m>0)
	gen mig = (l_prime<11)
	replace mig = . if l_prime == 99 //important adjustment
	local vars `"x f married h wage mig"'

	collapse (mean) `vars', by(race age) //collapse
	foreach var in `vars'{
		ren `var' `var'`i'
	}
	save "$temp/sample`i'_collapsed", replace
}



use "$temp/sample1_collapsed", clear
merge 1:1 race age using "$temp/sample2_collapsed", keep(match) nogen


local words `"Fertility "'
local vars `"f"'
local counter 0

//make some plots
foreach var in `vars'{
    local counter `++counter'
	local word = word("`words'", `counter')
    line `var'1 age if race == 1, lc(navy) || ///
	line `var'2 age if race == 1, lc(navy) lp(dash) || ///
	line `var'1 age if race == 2, lc(maroon) || ///
	line `var'2 age if race == 2, lc(maroon) lp(dash) ///
	bgcolor(white) graphregion(color(white))  ///
	legend(lab(1 "Whites, Data") lab(2 "Whites, Model") lab(3 "Blacks, Data") lab(4 "Blacks, Model")) ytitle("`word'")
	graph export "$output/Figure_5b.png", replace
}


local words `"Married "'
local vars `"married"'
local counter 0

//make some plots
foreach var in `vars'{
    local counter `++counter'
	local word = word("`words'", `counter')
    line `var'1 age if race == 1, lc(navy) || ///
	line `var'2 age if race == 1, lc(navy) lp(dash) || ///
	line `var'1 age if race == 2, lc(maroon) || ///
	line `var'2 age if race == 2, lc(maroon) lp(dash) ///
	bgcolor(white) graphregion(color(white))  ///
	legend(lab(1 "Whites, Data") lab(2 "Whites, Model") lab(3 "Blacks, Data") lab(4 "Blacks, Model")) ytitle("`word'")
	graph export "$output/Figure_A3b.png", replace
}



graph close



//end of dofile